﻿using LiveChartsCore;
using LiveChartsCore.ConditionalDraw;
using LiveChartsCore.Defaults;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.SkiaSharpView.Painting.Effects;
using Prism.Commands;
using Prism.Mvvm;
using SkiaSharp;
using System.Collections.ObjectModel;

namespace LiveCharts2_Demo.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        public DelegateCommand<string> Command { get; }
        public MainWindowViewModel()
        {
            Command = new DelegateCommand<string>(Execute);
        }
        private string _title = "Prism Application";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        //线集合
        private ObservableCollection<ISeries> listSeries = new();
        public ObservableCollection<ISeries> ListSeries
        {
            get { return listSeries; }
            set { SetProperty(ref listSeries, value); }
        }
        //X轴
        public Axis[] XAxes { get; set; }
           = new Axis[]
           {
                new Axis
                {
                    Name = "X轴",
                    NamePaint = new SolidColorPaint(SKColors.Black),
                    LabelsPaint = new SolidColorPaint(SKColors.Blue),
                    TextSize = 10,
                    MinLimit = 1,
                    MaxLimit = 100,
                    MinStep = 1,
                    SeparatorsPaint = new SolidColorPaint(SKColors.LightSlateGray) { StrokeThickness = 2 }
                }
           };
        //Y轴
        public Axis[] YAxes { get; set; }
            = new Axis[]
            {
                new Axis
                {
                    Name = "X轴",
                    NamePaint = new SolidColorPaint(SKColors.Red),
                    LabelsPaint = new SolidColorPaint(SKColors.Green),
                    TextSize = 20,
                    SeparatorsPaint = new SolidColorPaint(SKColors.LightSlateGray)
                    {
                        StrokeThickness = 2,
                        PathEffect = new DashEffect(new float[] { 3, 3 })
                    }
                }
            };
        //背景色
        public DrawMarginFrame DrawMarginFrame => new DrawMarginFrame
        {
            Fill = new SolidColorPaint(SKColors.White),
            Stroke = new SolidColorPaint(SKColors.Black, 3)
        };
        //水平线、垂直线
        public RectangularSection[] Sections { get; set; }
           = new RectangularSection[]
           {
                new RectangularSection
                {
                    // creates a section from 3 to 4 in the X axis
                    Xi = 3,
                    Xj = 3,
                    Stroke = new SolidColorPaint
            {
                Color = SKColors.Red,
                StrokeThickness = 3,
                //PathEffect = new DashEffect(new float[] { 6, 6 })
            }
                },

                //new RectangularSection
                //{
                //    // creates a section from 5 to 6 in the X axis
                //    // and from 2 to 8 in the Y axis
                //    Xi = 5,
                //    Xj = 6,
                //    Yi = 2,
                //    Yj = 8,
                //    Fill = new SolidColorPaint(new SKColor(187, 222, 251))
                //},

                // new RectangularSection
                //{
                //    // creates a section from 8 to the end in the X axis
                //    Xi = 8,
                //    Fill = new SolidColorPaint(new SKColor(249, 251, 231))
                //},

                  new RectangularSection
        {
            Yi = 3,
            Yj = 3,
            Label="YYY",
            Stroke = new SolidColorPaint
            {
                Color = SKColors.Red,
                StrokeThickness = 3,
                //PathEffect = new DashEffect(new float[] { 6, 6 })
            }
        }
           };
        //点颜色数组
        SolidColorPaint[] paintsStyle = new SolidColorPaint[]
       {
            new(SKColors.Red),
            new(SKColors.Green),
            new(SKColors.Blue),
            new(SKColors.Yellow)
       };
        //方法
        private void Execute(string name)
        {
            switch (name)
            {
                case "look":
                    {
                        this.ListSeries.Clear();
                        this.ListSeries.Add(new LineSeries<ObservablePoint>
                        {
                            Name = "荷花",
                            Stroke = new SolidColorPaint(SKColors.Blue, 3),
                            Values = new ObservableCollection<ObservablePoint> {
                                new(2.2, 5.4),
                                new(3, 2.5),
                                new(4.2, 7.4),
                                new(6.4, 9.9),
                                new(7, 9.2),
                                new(8.5, 3.5),},
                            Fill = null,
                            GeometrySize = 10,
                            LineSmoothness = 0
                        }.OnPointMeasured(point => //画的每一个点都可以控制显示样式
                        {
                            if (point.Visual is null) return;
                            var paint = paintsStyle[point.Index % paintsStyle.Length];
                            point.Visual.Fill = paint;
                        }));
                        //SVGPathGeometry
                        this.ListSeries.Add(new LineSeries<ObservablePoint, LiveChartsCore.SkiaSharpView.Drawing.Geometries.RectangleGeometry>
                        {
                            Name = "荷花",
                            Stroke = new SolidColorPaint(SKColors.YellowGreen, 3),
                            Values = new ObservableCollection<ObservablePoint>{
                                new(12.2, 5.4),
                                new(13, 2.5),
                                new(14.2, 7.4),
                                new(16.4, 9.9),
                                new(17, 9.2),
                                new(18.5, 3.5),},
                            Fill = null,
                            GeometrySize = 10,
                            LineSmoothness = 1
                        }.OnPointMeasured(point => //画的每一个点都可以控制显示样式
                        {
                            if (point.Visual is null) return;
                            var paint = paintsStyle[point.Index % paintsStyle.Length];
                            point.Visual.Fill = point.Model?.Y > 8 ? paintsStyle[0] : null;
                        }));

                        this.ListSeries.Add(new LineSeries<ObservablePoint, LiveChartsCore.SkiaSharpView.Drawing.Geometries.RectangleGeometry>
                        {
                            Name = "荷花",
                            Values = new ObservablePoint[] {
                                new(22.2, 5.4),
                                new(23, 2.5),
                                new(24.2, 7.4),
                                new(26.4, 9.9),
                                new(27, 9.2),
                                new(28.5, 3.5),},
                            Fill = new SolidColorPaint(SKColors.CornflowerBlue),
                            GeometryStroke = null,
                            Stroke = null,
                            GeometryFill = null,
                            LineSmoothness = 1
                        });
                    }
                    break;
                case "add":
                    this.ListSeries.Add(new LineSeries<double>
                    {
                        Values = new double[] { 2, 1, 4, 2, 2, -5, -2 },
                        Fill = null,

                        GeometrySize = 10
                    });
                    break;
            }

        }



    }
}
